<?php

require_once ("IDecoratedPage.php");

/**
 * A decorator class for Page class
 * Implements IDecorator
 * Will strip content of body to be better suited for mobile view
 * Takes a Page as argument i constructor
 */
class StrippedPage implements IDecoratedPage {

	public $title = "";
	public $body = "";
	public $menu = "";

	public function __construct(IDecoratedPage $page) {
		$this -> body = $page -> body;
		$this -> title = $page -> title;
		$this -> menu = $page -> menu;
		$this -> modifyForMobileView();
	}
	
	/**
	 * Will call on private function to remove img tags from self->body
	 * for mobil presentation
	 * 
	 * @access private
	 * @param -
	 * @return -
	 */
	private function modifyForMobileView() {
		$this->body = $this->removeTags($this->body, "<img>");
	}
	
	/**
	 * Removes tags from a string, can be served string with taggs or array
	 * @access private
	 * @param string to be cleaned, string or array with taggs to remove
	 */
	private function removeTags($str, $tags) {
		if (!is_array($tags)) {
			$tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
			if (end($tags) == '')
				array_pop($tags);
		}
		foreach ($tags as $tag)
			$str = preg_replace('#</?' . $tag . '[^>]*>#is', '', $str);
		return $str;
	}

}
